from selenium import webdriver
from time import sleep # 强制等待
from selenium.webdriver.common.keys import Keys   # 键盘操作
from selenium.webdriver.common.by import By       # 定位方式
from selenium.webdriver.common.action_chains import ActionChains  # 鼠标操作
from selenium.webdriver.support.select import Select   # 下拉框
from selenium.webdriver.support import expected_conditions as EC   # 判断元素是否存在/可见
from selenium.webdriver.support.wait import WebDriverWait        #显示等待

class Broswer():

    def __init__(self,type="gc"):   #浏览器类型
        if type=="gc":
            self.driver=webdriver.Chrome()
        elif type=="ie":
            self.driver=webdriver.Ie()
        elif type =="ff":
            self.driver=webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
    def open_url(self,url):
        self.driver.get(url)

    def __get_element(self,location):
        element = self.driver.find_element(*location)
        return element

    def click_element(self,location):
        ele = self.__get_element(location)
        ele.click()

    def input_text(self,location,value):
        ele = self.__get_element(location)
        ele.send_keys(value)

    def switch_to_window(self,idx=-1):
        self.driver.switch_to.window(self.driver.window_handles[idx])

    def switch_into_frame(self,location):
        frame = self.__get_element(location)
        self.driver.switch_to.frame(frame)

    def switch_out_frame(self):
        self.driver.switch_to.default_content()

    def switch_to_alert(self,action="a",value=None):
        alert = self.driver.switch_to.alert
        if action =="a":
            alert.accept()
        elif action == "d":
            alert.dismiss()
        else:
            alert.send_keys(value)
            alert.accept()
    def selected(self,location,idx=None,value=None,text=None):
        ele = self.__get_element(location)
        select = Select(ele)

        if idx is not None:
            select.select_by_index(idx)
        if value is not None:
            select.select_by_value(value)
        if text is not None:
            select.select_by_visible_text(text)
    def action_chains(self,location,action):
        ele = self.__get_element(location)
        ac = ActionChains(self.driver)

        if action == "move":
            ac.move_to_element(ele)
        elif action == "double":
            ac.double_click()
        elif action == "right":
            ac.context_click()
    def get_screenshoot_as_file(self,filename):
        self.driver.get_screenshot_as_file(filename)
    def keys_action(self,location,key1,key2=None,key3=None):
        ele = self.__get_element(location)
        if key1 == "ctr1" and key2 is not None:
            ele.send_keys(Keys.CONTROL,key2)
        elif key1 == "ctr1" and key2 is not None and key3 is not None:
            ele.send_keys(Keys.CONTROL,key2,key3)
        elif key1 == "del":
            ele.send_keys(Keys.DELETE)

    def clear_element(self,location):
        ele = self.__get_element(location)
        ele.clear()
























